package com.customization.assets.formmode.remind;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.customization.assets.util.AbstractCronJob;
import com.customization.assets.util.AssetRemind;
import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.soa.workflow.request.RequestService;
import weaver.system.SysRemindWorkflow;

import com.customization.assets.util.DBTools;
import com.customization.assets.util.DateTools;

/**
 * @author Huangwm
 * @description 实物资产使用年限提醒,超过6年的开始提示管理员;在用或者借用的,获取总数提示对应部门管理员；在库的提示系统管理员
 */
public class UseYearRemind extends AbstractCronJob {

	private LogTool log = new LogTool("/log/assetlog/huangwm/UseYearRemind",false);
	private DateTools dateTools = new DateTools();
	private DBTools dbTools = new DBTools();

	public String customid = "";
	public String getcustomid(){
		return customid;
	}
	public void setcustomid(String customid){
		this.customid = customid;
	}
	
	public String logmodeid = "";
	public String getlogmodeid(){
		return logmodeid;
	}
	public void setlogmodeid(String logmodeid){
		this.logmodeid = logmodeid;
	}
	
	public String logformid = "";
	public String getlogformid(){
		return logformid;
	}
	public void setlogformid(String logformid){
		this.logformid = logformid;
	}
	@Override
	public void start() throws  Exception{
        log.writeLog("实物资产使用年限提醒开始" + dateTools.getCurrentTimes());
		List<Map<String, String>> useCount = getUseCount();
		if (useCount.size() > 0) {
			String title = "设备使用年限提醒" + dateTools.getCurrentDate();
			String remindContent = "";
			for (Map<String, String> map : useCount) {
				String count = map.get("counts");
				String dybm = map.get("dybm");
				String link = "&nbsp;" + "<a href=\"javascript:window.open(\'/spa/cube/index.html#/main/cube/search?customid=" + this.customid + "\')\">设备使用年限列表</a>&nbsp;";
				remindContent = "您所在部门下登记的实物资产使用年限达到6年的总数有" + count + "台; 请到" + link + "查看";
				String admin = getAsstesAdmin_sw(dybm);
                log.writeLog("管理员id "+admin);
				SysRemindWorkflow sysRemindWorkflow = new SysRemindWorkflow();
				ModeRightInfo mri = new ModeRightInfo();
				if ("".equals(admin) || "null".equals(admin)) {
                    log.writeLog("未找到部门管理员 插入数据到异常提醒建模表中");
					String uuid = dbTools.getUUID();
					String insertMode = "insert into uf_AssetRemindLog(formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,MODEUUID,gnly,jctxsj,tsnr,yczcsb,txbm) values(";
					insertMode += this.logmodeid + ",1,0,'" + dateTools.getCurrentDate() + "','" + dateTools.getCurrentTime() + "','" + uuid + "',3,'" + dateTools.getCurrentDate() + " " + dateTools.getCurrentTime() + "','您所在部门下登记的实物资产使用年限达到6年的总数有" + count + "台; 请到设备使用年限列表查看',''," + dybm + ")";
					RecordSet rs1 = new RecordSet();
					if (rs1.execute(insertMode)) {
                        log.writeLog(insertMode);
						String getBillid = "select id from uf_AssetRemindLog where MODEUUID='" + uuid + "'";
						RecordSet rs2 = new RecordSet();
						if (rs2.execute(getBillid) && rs2.next()) {
                            log.writeLog(getBillid + ">>>" + rs2.getInt("id"));
							mri.editModeDataShare(1, Integer.parseInt(this.logformid), rs2.getInt("id"));
                            log.writeLog("数据进行权限重构");
						}
					}
				} else {
					try {
						//sysRemindWorkflow.setCRMSysRemind(title, 0, 1, admin,remindContent);
						Map<String, String> mainData = new HashMap<>();
						mainData.put("txr", "1");
						mainData.put("tzr", admin);
						mainData.put("bz", remindContent);
						mainData.put("xgzc", "");
						mainData.put("xglc", "-1");
						mainData.put("xgbm", "");
						AssetRemind assetRemind = new AssetRemind();
						RequestService service = new RequestService();
						String requestid = service.createRequest(assetRemind.setRequestInfo("1", title, mainData));
                        log.writeLog("实物资产使用年限提醒>" + admin + " requestid=" + requestid);
					} catch (Exception e) {
						e.printStackTrace();
                        log.writeLog("发送提醒异常  " + e.getMessage());
					}
				}
			}
		}
		Integer noUseCount = getNoUseCount();
		log.writeLog("实物资产使用年限提醒结束" + dateTools.getCurrentTimes());
	}
	/**
	 * 获取实物资产中存在对应部门的数据
	 * @return
	 */
	public List<Map<String, String>> getUseCount(){
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		RecordSet rs = new RecordSet();
		String sql = "select count(id) as counts,dybm from uf_swzczmxx where dybm<>'' and DATEDIFF(YEAR,rkrq,GETDATE())>=6 group by dybm";
		rs.execute(sql);
		while(rs.next()){
			Map<String, String> data = new HashMap<String, String>();
			data.put("dybm", rs.getString("dybm"));
			data.put("counts", rs.getString("counts"));
			list.add(data);
		}
		return list;
	}
	
	/**
	 * 获取实物资产中非报废且没有对应部门的数量
	 * @return
	 */
	public Integer getNoUseCount(){
		RecordSet rs = new RecordSet();
		String sql = "select count(id) as counts from uf_swzczmxx where zczt <> 3 and DATEDIFF(YEAR,rkrq,GETDATE())>=6 and ( dybm='' or dybm is null)";
		rs.execute(sql);
		if(rs.next()){
			return rs.getInt(1);
		}
		return 0;
	}
	
	/**
	 * 获取实物资产卡片中所在部门的部门资产管理员
	 * @param szbm
	 * @return
	 */
	public String getAsstesAdmin_sw(String szbm){
		RecordSet rs = new RecordSet();
		String assetsAdmin = "";
		String sql = "select swzcgly from Matrixtable_1 where id="+szbm;
		rs.execute(sql);
		while(rs.next()){
			assetsAdmin = rs.getString("swzcgly");
		}
		return assetsAdmin;
	}
}
